<template>
  <div class="search">
    <el-form :model="searchForm" ref="formRef" label-width="80px" size="large">
      <el-row :gutter="20">
        <template v-for="item in searchConfig.formItems" :key="item.prop">
          <el-col v-show="item.type === 'input'" :span="8">
            <el-form-item :label="item.label" :prop="item.prop">
              <el-input
                v-model="searchForm[item.prop]"
                :placeholder="item.placeholder"
              />
            </el-form-item>
          </el-col>
          <el-col v-show="item.type === 'date-picker'" :span="8">
            <el-form-item :label="item.label" :prop="item.prop">
              <el-date-picker
                v-model="searchForm[item.prop]"
                type="daterange"
                range-separator="-"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
              />
            </el-form-item>
          </el-col>
        </template>
      </el-row>
    </el-form>
    <!-- 按钮 -->
    <div class="btns">
      <el-button icon="Refresh" size="large" @click="handleResetClick"
        >重置</el-button
      >
      <el-button
        icon="Search"
        type="primary"
        size="large"
        @click="handleQueryClick"
        >查询</el-button
      >
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import type { ElForm } from 'element-plus'

interface IProp {
  searchConfig: { formItems: any[] }
}
// 发射事件/prop
const emit = defineEmits(['queryClick', 'resetClick'])
const props = defineProps<IProp>()

// 定义表单数据
const initalForm: any = {}
for (const item of props.searchConfig.formItems) {
  initalForm[item.prop] = item.initalValue ?? ''
}

const searchForm = reactive(initalForm)
// 重置
const formRef = ref<InstanceType<typeof ElForm>>()
function handleResetClick() {
  formRef.value?.resetFields()
  emit('resetClick')
}
function handleQueryClick() {
  emit('queryClick', searchForm)
}
</script>

<style lang="scss" scoped>
.search {
  background-color: #fff;
  padding: 20px;
  .el-form-item {
    padding: 20px 30px;
    margin-bottom: 0;
  }
}
.btns {
  text-align: right;
  padding: 0 50px 10px 0;
  .el-button {
    height: 36px;
  }
}
</style>
